package com.atguigu.tingshu.account.service.impl;

import com.atguigu.tingshu.account.mapper.UserAccountDetailMapper;
import com.atguigu.tingshu.account.mapper.UserAccountMapper;
import com.atguigu.tingshu.account.service.UserAccountService;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.model.account.UserAccount;
import com.atguigu.tingshu.model.account.UserAccountDetail;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	@Autowired
	private UserAccountMapper userAccountMapper;

	@Autowired
	private UserAccountDetailMapper userAccountDetailMapper;
	/**
	 *  初始化用户账户
	 * @param userId
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void initUserAccount(Long userId) {
		//构建账户对象
		UserAccount userAccount = new UserAccount();
		userAccount.setUserId(userId);
		userAccount.setTotalAmount(new BigDecimal(100));
		userAccount.setAvailableAmount(new BigDecimal(100));
		userAccount.setTotalIncomeAmount(new BigDecimal(100));
		//初始化账户
		userAccountMapper.insert(userAccount);

		//新增账户明细
		this.saveUsesrAccoutnDetail(
				userId,
				"开通账户，赠送100元",
				SystemConstant.ACCOUNT_TRADE_TYPE_DEPOSIT,
				new BigDecimal(100),
				null
		);

	}

	/**
	 * 新增账户记录
	 * @param userId
	 * @param title
	 * @param tradeType
	 * @param amount
	 * @param orderNo
	 */
	public void saveUsesrAccoutnDetail(Long userId,String title,String tradeType,BigDecimal amount,String orderNo){

		//创建记录对象
		UserAccountDetail userAccountDetail = new UserAccountDetail();
		userAccountDetail.setUserId(userId);
		userAccountDetail.setTitle(title);
		userAccountDetail.setTradeType(tradeType);
		userAccountDetail.setAmount(amount);
		userAccountDetail.setOrderNo(orderNo);

		//新增记录
		userAccountDetailMapper.insert(userAccountDetail);


	}



}
